<?php
defined ( 'IN_TS' ) or die ( 'Access Denied.' );  
$list = array();
switch ($ts) {
	case 'index':
		  //首页显示待处理订单：超管看到全部待处理，普通理发师看到的是个人的待处理订单
		$ism = $_GET[ 'ism' ] ? $_GET[ 'ism' ] : 0 ;//1超管0普通
		$hid = $_GET[ 'hid' ] > 0 ? $_GET[ 'hid' ] : 1 ;//登录人id
		 
		$dataList = array();
		$conStr = '1=1';
		if( $ism == 0 && $hid > 0 ){
			$conStr .= ' and hairdresser_id='.$hid.' and order_status=0';
		
		}elseif( $ism == 1 && $hid > 0 ){
			$conStr .= ' and order_status=0';
		}
		//echo $conStr;
		
		$dataList = $new[ 'home' ]->findAll('orders',$conStr );
		$rc = requireclass( "user" );		
		foreach( $dataList as $key => $val ){
			$hinfo = $rc->caHairdresser( $val[ 'hairdresser_id' ] );
			$sinfo = $rc->caServiceItem( $val[ 'item_id' ] ) ;
			$out[ $key ][ 'subscribe_time' ] = date('m/d H:i',$val[ 'subscribe_time' ] );
			$out[ $key ][ 'create_at' ] = date('m/d H:i',$val[ 'create_at' ] );
			$out[ $key ][ 'mphone' ] = $val[ 'mphone' ] ;
			$out[ $key ][ 'hairdresser_id' ] = $hinfo[ 'hairdresser_id' ]; 
			$out[ $key ][ 'name' ] = $hinfo[ 'name' ];
			$out[ $key ][ 'mnickname' ] = $val[ 'mnickname' ] ;
			$out[ $key ][ 'order_id' ] = $sinfo[ 'order_id' ] ;
			$out[ $key ][ 'item_id' ] = $sinfo[ 'item_id' ] ;
			$out[ $key ][ 'item_name' ] = $sinfo[ 'item_name' ] ;
		}   
		if( !empty( $dataList ) ){
			echo tojson1( $out );
		}else{
			echo '{"result":0,"message":"无记录！"}';exit;
		}
		
	break;
	case 'order_list':
		//超管看全部的订单— 
		$ism = $_GET[ 'ism' ] ? $_GET[ 'ism' ] : 0 ;//1超管0普通
		$hid = $_GET[ 'hid' ] > 0 ? $_GET[ 'hid' ] : 1 ;//登录人id
		if( !$hid ){
			echo '{"result":0,"message":"无效参数！"}';exit;
		}
		$dataList = array();
		$conStr = '1=1 and jsstat=0';
		if( !$ism ){
			$conStr .= ' and hairdresser_id='.$hid;		
		}else{
			$conStr .= '';
		}
		$dataList = $new[ 'home' ]->findAll('orders',$conStr,'order_code desc' );
		$rc = requireclass( "user" );		
		foreach( $dataList as $key => $val ){
			$hinfo = $rc->caHairdresser( $val[ 'hairdresser_id' ] );
			$sinfo = $rc->caServiceItem( $val[ 'item_id' ] ) ;
			$out[ $key ][ 'subscribe_time' ] = date('m/d H:i',$val[ 'subscribe_time' ] );
			$out[ $key ][ 'create_at' ] = date('m/d H:i',$val[ 'create_at' ] );
			$out[ $key ][ 'mphone' ] = $val[ 'mphone' ] ;
			$out[ $key ][ 'mnickname' ] = $val[ 'mnickname' ] ;
			$out[ $key ][ 'order_id' ] = $val[ 'order_id' ] ;
			$out[ $key ][ 'order_status' ] = $val[ 'order_status' ] ;
			
			$out[ $key ][ 'hairdresser_id' ] = $hinfo[ 'hairdresser_id' ]; 
			$out[ $key ][ 'name' ] = $hinfo[ 'name' ];
			$out[ $key ][ 'item_id' ] = $sinfo[ 'item_id' ] ;
			$out[ $key ][ 'item_name' ] = $sinfo[ 'item_name' ] ;
		}   
		if( !empty( $dataList ) ){
			echo tojson1( $out );
		}else{
			echo '{"result":0,"message":"无记录！"}';exit;
		}
	
	break;
	//标记订单为已处理/退单
	case 'order_opt':
		$stat = $_GET[ 'stat' ] > 0 ? intval( $_GET[ 'stat' ] ) : 0;//订单状态，0待处理，1已处理，2 退单
		$order_id = $_GET[ 'order_id' ] ? intval( $_GET[ 'order_id' ] ) : 0 ; 
		$data = array(
			'order_status' => $stat,
			'complete_time' => time(),//更新时间：退单或者结束订单
		
		);

		$ism = $_GET[ 'ism' ] ? $_GET[ 'ism' ] : 0 ;//1超管0普通
		$hid = $_GET[ 'hid' ] > 0 ? $_GET[ 'hid' ] : 1 ;//登录人id 
		$dataList = array();
		$conStr = '';
		if( !$ism ){
			$conStr = 'hairdresser_id='.$hid;
		}else{
			$conStr = '';
		}
		$dataList = $new[ 'home' ]->findAll('orders',$conStr );
		$rc = requireclass( "user" );		
		foreach( $dataList as $key => $val ){
			$hinfo = $rc->caHairdresser( $val[ 'hairdresser_id' ] );
			$sinfo = $rc->caServiceItem( $val[ 'item_id' ] ) ;
			$out[ $key ][ 'subscribe_time' ] = date('m/d H:i',$val[ 'subscribe_time' ] );
			$out[ $key ][ 'create_at' ] = date('m/d H:i',$val[ 'create_at' ] );
			$out[ $key ][ 'mphone' ] = $val[ 'mphone' ] ; 
			$out[ $key ][ 'order_id' ] = $val[ 'order_id' ] ;
			$out[ $key ][ 'order_status' ] = $val[ 'order_status' ] ;
			
			$out[ $key ][ 'hairdresser_id' ] = $hinfo[ 'hairdresser_id' ]; 
			$out[ $key ][ 'name' ] = $hinfo[ 'name' ];
			$out[ $key ][ 'item_id' ] = $sinfo[ 'item_id' ] ;
			$out[ $key ][ 'item_name' ] = $sinfo[ 'item_name' ] ;
		}   
		
		
		if( $stat == 1 || $stat == 2 ){
			$result = $new[ 'home' ]->update('orders',array( 'order_id' => $order_id ),$data);
		}
		if( !empty( $dataList ) ){
			echo tojson1( $out ); 
		}else{
			echo '{"result":0,"message":"无权查看！"}';exit;
		}  
	break;
	//项目添加
	case 'item_add' : 
		$data = array();
		//$data[ 'item_name' ] = $_GET[ 'item_name' ] ? h( $_GET[ 'item_name' ] ) : '' ;
		//$data[ 'item_time' ] = $_GET[ 'item_time' ] ;//填写分钟
		$data[ 'item_name' ] = $_GET[ 'item_name' ] ? h( $_GET[ 'item_name' ] ) : '' ;
		$data[ 'item_time' ] = $_GET[ 'item_time' ] ;//填写分钟
		$data[ 'create_at'] = time();  
		$isExi = $new[ 'home' ]->find('service_item','item_name="'.$data[ 'item_name' ].'"');
		$list = $new[ 'home' ]->findAll('service_item');
		$opt = array();
		if( empty( $isExi ) ){
			$lastId = $new[ 'home' ]->create( 'service_item' , $data );
		} 
		
		if( !$lastId ){
			//失败
			$opt = array(
				'result' => 0,				
				'list' => $list
			
			); 
			echo tojson1( $opt );exit;
		}else{
			//成功
			$opt = array(
				'result' => 1,				
				'list' => $list
			
			); 
			echo tojson1( $opt );exit;
		}

	break;
	//项目列表
	case 'item_list':
		$ism = $ism == 1 ? $ism : 1 ;
		
		if( !$ism ){
			echo '{"result":0,"message":"无权查看！"}';exit;
		}
		$list = array();
		$list = $new[ 'home' ]->findAll('service_item');
		$opt = array(
			'result' => 1,
			'list' => $list
		);		

		echo tojson1($opt);
	break;
	//修改项目，不许删除
	case 'item_update':
		extract( $_GET );
		$data = array();
		$data[ 'item_name' ] = $item_name ? h( $item_name ) : '' ;
		$data[ 'item_time' ] = $item_time ;//填写分钟
		$data[ 'item_id' ] = intval( $item_id );
		if( !$data[ 'item_id' ] ){
			echo '{"result":0,"message":"无效参数！"}';exit;
		}
		$new[ 'home' ]->update('service_item','item_id='.$data[ 'item_id' ],$data);
		break;
	//店员管理-list
	case 'haird_list':
		$list = array();
		$list = $new[ 'home' ]->findAll('hairdresser','type_id=1 and stat=0 and ism = 0','hairdresser_id desc');
		echo tojson1( $list );		
		break;
	case 'haird_add' :
			/*
		$data[ 'haird_num' ] = $_GET[ 'haird_num' ];
		$data [ 'name' ] = $_GET[ 'name' ];
		$data[ 'level_id' ] = $_GET[ 'level_id' ] ? $_GET[ 'level_id' ] : 2 ;
		$data[ 'picture' ] = '../../img/photo/photo_1.jpg';
		$data[ 'create_at' ] = time();
		$data[ 'phone_num' ] = $_GET[ 'phone_num' ];
		*/

		if($_POST)
		{
			$data[ 'haird_num' ] = $_POST[ 'haird_num' ];
			$data [ 'name' ] = $_POST[ 'name' ];
			$data[ 'level_id' ] = $_POST[ 'level_id' ] ? $_POST[ 'level_id' ] : 1 ;
			$data[ 'create_at' ] = time();
			$data[ 'type_id' ] = 1;
			$data[ 'stat' ] = 0;
			$data[ 'ism' ] = 0;
			$data[ 'phone_num' ] = $_POST[ 'phone_num' ];
			if(isset($_FILES['file'])){
				$info = uploadpic($_FILES['file'],1,'uploads/'.date('Y',time()).'/'.date('m',time()).'/'.date('d',time()));
				if( !$info ){

				}else{


					$data[ 'picture' ] = $info['url'];
				}
			}
		}

		$lastId = $new[ 'home' ]->create('hairdresser',$data );
		$list = $new[ 'home' ]->findAll('hairdresser','type_id=1 and stat =0 and ism =0','hairdresser_id desc');



		
		if( $lastId > 0 ){
			$opt = array(
				'result' => 1,
				'list' => $list
			);
			echo tojson1( $opt );
		}else{
			$opt = array(
				'result' => 0,
				'list' => $list
			);
			echo tojson1( $opt );
		} 
		break;
	case 'haird_update' :
		$hid = $_GET[ 'hid' ] ;
		$data [ 'name' ] = $_GET[ 'name' ];
		$data[ 'level_id' ] = $_GET[ 'level_id' ] ? $_GET[ 'level_id' ] : 2 ; 
		$data[ 'create_at' ] = time();
		$data[ 'phone_num' ] = $_GET[ 'phone_num' ];		
		$data[ 'haird_num' ] = $_GET[ 'haird_num' ];
		$new[ 'home' ]->update('hairdresser','hairdresser_id='.$hid ,$data);
		echo '{"result":1,"message":"操作成功！"}';exit;
		break;
	case 'haird_del':
		$hid = $_GET[ 'hid' ] ; 
		$new[ 'home' ]->update('hairdresser','hairdresser_id='.$hid ,array('stat'=>1) );//设置为1等于删除
		$list = $new[ 'home' ]->findAll('hairdresser','type_id=1 and stat =0 and ism =0','hairdresser_id desc');
		$opt = array(
				'result' => 1,
				'list' => $list
			);
			echo tojson1( $opt );
		break;
	//店员项目管理-添加某个店员的某个项目
	case 'personal_item_list':
		$hid = intval( $_GET[ 'hid' ] );


		$out = array();
		$itemList = array();
		$list = $new[ 'home' ]->findAll('item_hairdresser_relation','hairdresser_id='.$hid);
		$rc = requireclass( 'user' );
		foreach( $list as $key => $val ){
			$titemInfo = $rc->caServiceItem( $val[ 'item_id' ] );
			$out[ $key ][ 'item_name' ] = $titemInfo[ 'item_name' ];
			$out[ $key ][ 'id' ] = $val[ 'id' ];
			$out[ $key ][ 'price' ] = $val[ 'price' ];
			$out[ $key ][ 'fencheng' ] = $val[ 'fencheng' ];
		}
		$itemList = $new[ 'home' ]->findAll('service_item');
		$opt = array(
			'result' => 1,
			'list' => $out,
			'itemList' => $itemList,
		);
		echo tojson1( $opt );
		break;
	case 'personal_item_add' :
		$data[ 'hairdresser_id' ] = intval( $_GET[ 'hid' ] ) > 0 ? intval( $_GET[ 'hid' ] ) : 1 ;
		$data[ 'item_id' ] = intval( $_GET[ 'item_id' ] ) > 0 ? intval( $_GET[ 'item_id' ] ) : 3;
		$data[ 'price' ] = $_GET[ 'price' ];
		$data[ 'fencheng' ] = $_GET[ 'fencheng' ];
		$data[ 'create_at' ] = time();  
		if( $data[ 'hairdresser_id' ] <= 0 ){
			echo '{"result":0,"message":"参数无效！"}';exit;
		}
		$new[ 'home' ]->create('item_hairdresser_relation',$data );
		$list = $new[ 'home' ]->findAll('item_hairdresser_relation','hairdresser_id='.$_GET[ 'hid' ]);
		$rc = requireclass( 'user' );
		foreach( $list as $key => $val ){
			$titemInfo = $rc->caServiceItem( $val[ 'item_id' ] );
			$out[ $key ][ 'item_name' ] = $titemInfo[ 'item_name' ];
			$out[ $key ][ 'id' ] = $val[ 'id' ];
			$out[ $key ][ 'price' ] = $val[ 'price' ];
			$out[ $key ][ 'fencheng' ] = $val[ 'fencheng' ];
		}
		$itemList = $new[ 'home' ]->findAll('service_item');
		$opt = array(
			'result' => 1,
			'list' => $out,
			'itemList' => $itemList,
		);
		echo tojson1( $opt );exit;
		echo '{"result":1,"message":"添加成功！"}';exit;
		break;
	case 'personal_item_update':
		$id = $_GET[ 'id' ];
		$hid = $_GET[ 'hid' ] ? intval( $_GET[ 'hid' ] ) : 1;
		$data[ 'item_id' ] = intval( $_GET[ 'item_id' ] );
		$data[ 'price' ] = $_GET[ 'price' ];
		$data[ 'fencheng' ] = $_GET[ 'fencheng' ];
		$new[ 'home' ]->update( 'item_hairdresser_relation','id='.$id,$data );
		
		$list = $new[ 'home' ]->findAll('item_hairdresser_relation','hairdresser_id='.$hid);
		    $rc = requireclass( 'user' );
			foreach( $list as $key => $val ){
				$titemInfo = $rc->caServiceItem( $val[ 'item_id' ] );
				$out[ $key ][ 'item_name' ] = $titemInfo[ 'item_name' ];
				$out[ $key ][ 'id' ] = $val[ 'id' ];
				$out[ $key ][ 'price' ] = $val[ 'price' ];
				$out[ $key ][ 'fencheng' ] = $val[ 'fencheng' ]; 
			}
			$opt = array(
				'result' => 1,
				'list' => $out			
			);
			echo tojson1( $opt ); 

		break;
	case 'personal_item_del':
		$id = $_GET[ 'id' ]; 
		$hid = $_GET[ 'hid' ] > 0 ? $_GET[ 'hid' ] : 1 ;
		$new[ 'home' ]->delete( 'item_hairdresser_relation','id='.$id );

		$list = $new[ 'home' ]->findAll('item_hairdresser_relation','hairdresser_id='.$hid);
		    $rc = requireclass( 'user' );
			foreach( $list as $key => $val ){
				$titemInfo = $rc->caServiceItem( $val[ 'item_id' ] );
				$out[ $key ][ 'item_name' ] = $titemInfo[ 'item_name' ];
				$out[ $key ][ 'id' ] = $val[ 'id' ];
				$out[ $key ][ 'price' ] = $val[ 'price' ];
				$out[ $key ][ 'fencheng' ] = $val[ 'fencheng' ]; 
			}
			$opt = array(
				'result' => 1,
				'list' => $out			
			);
			echo tojson1( $opt ); 

		break;
		//结算报表----结算报表显示，待结算总额，以及提成总额
	case 'js_list':

		$hid = intval( $_GET[ 'hid' ] );
		//普通人员查看自己的  
		$conStr = 'jsstat=0';

		if( $hid == 0 ){

			$conStr .= '';
		}else{
			$conStr .= ' and hairdresser_id='.$hid;
		}
		//echo $conStr;
		$list = $new[ 'home' ]->findAll('orders',$conStr);
		$rc = requireclass( "user" );
		$jsTotalMoney = 0;
		$jsFenchengMoney = 0;
		foreach( $list as $key => $val ){			
			$itemInfo = $rc->caServiceItem( $val[ 'item_id' ] );			
			$priceInfo = $rc->getPersonalMoney( $val[ 'hairdresser_id' ],$val[ 'item_id' ]);
			
			$out[ $key ][ 'item_name' ] = $itemInfo[ 'item_name' ];
			$out[ $key ][ 'price' ] = $priceInfo[ 'price' ];
			$out[ $key ][ 'fencheng' ] = $priceInfo[ 'fencheng' ];
			if( $val[ 'jsstat' ] == 1 ){
				$out[ $key ][ 'jsstat' ] = '已结算';
				$out[ $key ][ 'jstime' ] = date( 'm/d',$priceInfo[ 'jstime' ] );
			}else{
				$out[ $key ][ 'jsstat' ] = '未结算';
				$out[ $key ][ 'jstime' ] = date( 'm/d',$priceInfo[ 'jstime' ] );
			}
			$out[ $key ][ 'order_id' ] = $val[ 'order_id' ];
			$jsTotalMoney += $priceInfo[ 'price' ];
			$jsFenchengMoney += $priceInfo[ 'fencheng' ];
		} 
		$hairdList = $new[ 'home' ]->findAll('hairdresser','type_id=1 and stat =0 and ism =0','hairdresser_id desc');
		//print_r($out);exit;
		$opt = array(
			'result' => 1,
			'list' => $out,
			'jsTotalMoney' => $jsTotalMoney,
			'jsFenchengMoney' => $jsFenchengMoney,
			'hairdList' => $hairdList
		);
		 
		echo tojson1( $opt );

		break;
	//点击结算 -结算时间 ，字段状态
	case 'js_opt' :
			$id = intval( $_GET[ 'order_id' ] );
			$ism = isset( $_GET[ 'ism' ] ) ? intval( $_GET[ 'ism' ] ) : 0;
			$hid = intval( $_GET[ 'hid' ] );
			$data = array(
				'jsstat' => 1,
				'jstime' => time()
			);
			$new[ 'home' ]->update('orders','order_id='.$id , $data );
			echo '{"result":1,"message":"操作成功！"}';exit;
		break;
	
	case 'uploadpic':
			$file = $_FILES[ 'file' ];
			$projectid = 1;//默认为1即可
			$dir = '' ;
			$info = uploadpic($files,$projectid,$dir,$uptypes);
			if( !$info ){
				echo '{"result":0,"message":"上传失败！"}';exit;
			}else{
				echo tojson1($info);
			}
			
		break;

	case 'test':
			
			echo '{"result":1,"message":"测试成功！"}';exit;
		break;
	case 'get_setting':
		$confList = $new[ 'home' ]->findAll('set_conf');

		$data		   =	array();

		if($confList)
		{
			foreach($confList as $v)
			{
				$data[$v['column_name']]  =   $v['column_value'];
				if($v['column_name']=='store_banner'){
					$data['store_banner']	=	$GLOBALS['_api_url'].$v['column_value'];
				}
				if($v['column_name']=='store_qr_code'){
					$data['store_qr_code']	=	$GLOBALS['_api_url'].$v['column_value'];
				}
			}

		}

		echo tojson1($data); exit;

		break;
	case 'setting':
		//uploadpic
		if($_POST )
		{
			if(isset($_POST['is_online_payment']) && isset($_POST['store_name']) && isset($_POST['store_address']) && isset($_POST['store_iphone']) && isset($_POST['store_time'])){
				$new[ 'home' ]->update('set_conf','column_name ="is_online_payment"' , array('column_value'=>$_POST['is_online_payment']) );
				$new[ 'home' ]->update('set_conf','column_name ="store_name"' , array('column_value'=>$_POST['store_name']) );
				$new[ 'home' ]->update('set_conf','column_name ="store_address"' , array('column_value'=>$_POST['store_address']) );
				$new[ 'home' ]->update('set_conf','column_name ="store_iphone"' , array('column_value'=>$_POST['store_iphone']));
				list($stat,$end) = explode('-',$_POST['store_time']);
				$new[ 'home' ]->update('set_conf','column_name ="start_time"' , array('column_value'=>$stat));
				$new[ 'home' ]->update('set_conf','column_name ="end_time"' , array('column_value'=>$end));



			}


		}
	if($_FILES ){
		if(isset($_FILES['store_banner'])){
			$info = uploadpic($_FILES['store_banner'],1,'uploads/'.date('Y',time()).'/'.date('m',time()).'/'.date('d',time()));
			if( !$info ){

			}else{
				$new[ 'home' ]->update('set_conf','column_name ="store_banner"' , array('column_value'=>$info['url']));

			}
		}
		if(isset($_FILES['qr_code'])){
			$info = uploadpic($_FILES['qr_code'],1,'uploads/'.date('Y',time()).'/'.date('m',time()).'/'.date('d',time()));
			if( !$info ){

			}else{
				$new[ 'home' ]->update('set_conf','column_name ="store_qr_code"' , array('column_value'=>$info['url']));

			}
		}
	}
		$data = array();
		$data['is_online_payment'] = $_POST['is_online_payment'];
		$data['store_name'] = $_POST['store_name'];
		$data['store_address'] = $_POST['store_address'];
		$data['store_iphone'] = $_POST['store_iphone'];
		$data['start_time'] = $stat;
		$data['end_time'] = $end;
		echo json_encode($data); exit;
	break;


	case 'noimg_setting':
		//uploadpic
		$_POST	=	json_decode(file_get_contents("php://input"),1);
		if($_POST )
		{
			if(isset($_POST['is_online_payment']) && isset($_POST['store_name']) && isset($_POST['store_address']) && isset($_POST['store_iphone']) && isset($_POST['store_time'])){
				$new[ 'home' ]->update('set_conf','column_name ="is_online_payment"' , array('column_value'=>$_POST['is_online_payment']) );
				$new[ 'home' ]->update('set_conf','column_name ="store_name"' , array('column_value'=>$_POST['store_name']) );
				$new[ 'home' ]->update('set_conf','column_name ="store_address"' , array('column_value'=>$_POST['store_address']) );
				$new[ 'home' ]->update('set_conf','column_name ="store_iphone"' , array('column_value'=>$_POST['store_iphone']));
				list($stat,$end) = explode('-',$_POST['store_time']);
				$new[ 'home' ]->update('set_conf','column_name ="start_time"' , array('column_value'=>$stat));
				$new[ 'home' ]->update('set_conf','column_name ="end_time"' , array('column_value'=>$end));



			}


		}

		$data = array();
		$data['is_online_payment'] = $_POST['is_online_payment'];
		$data['store_name'] = $_POST['store_name'];
		$data['store_address'] = $_POST['store_address'];
		$data['store_iphone'] = $_POST['store_iphone'];
		$data['start_time'] = $stat;
		$data['end_time'] = $end;
		echo json_encode($data); exit;
		break;
	case 'leves_get_all':
		$levels =   $new[ 'home' ]->findAll('levels');//Levels::All();
		if($levels)
		{
			echo tojson1($levels);exit;
		}else{
			echo '{"result":0,"message":"参数错误！"}';//return response()->json((object)array(),200);
		}
		break;
	case 'leves_get_all_arr':

		$levels =   $new[ 'home' ]->findAll('levels');//Levels::All();

		$data   = 	array();
		if($levels)
		{
			foreach($levels as $v)
			{
				$data[]	=	$v['name'];
			}
			echo tojson1($data);exit;
		}else{
			echo '{"result":0,"message":"参数错误！"}';//return response()->json((object)array(),200);
		}
		break;


}